package com.lagou.controller;

import com.lagou.domain.Course;
import com.lagou.domain.CourseLesson;
import com.lagou.domain.CourseSection;
import com.lagou.domain.ResponseResult;
import com.lagou.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    /**
     * 查询课程内容
     *
     * @param courseId 课程id
     * @return
     */
    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLessonByCourseId(Integer courseId) {

        List<CourseSection> list = courseContentService.findSectionAndLessonByCourseId(courseId);
        return new ResponseResult(true, 200, "查询课程章节及课时信息成功", list);
    }

    /**
     * 回显章节对应的课程信息
     *
     * @param courseId 课程id
     * @return
     */
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(Integer courseId) {

        Course course = courseContentService.findCourseByCourseId(courseId);
        return new ResponseResult(true, 200, "查询课程信息成功", course);
    }

    /**
     * 保存或修改课程章节信息
     *
     * @param courseSection
     * @return
     */
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection) {

        String msg;
        if (courseSection.getId() == null) {

            courseContentService.saveSection(courseSection);
            msg = "新建章节信息成功";
        } else {

            courseContentService.updateSection(courseSection);
            msg = "修改章节信息成功";
        }
        return new ResponseResult(true, 200, msg, null);
    }

    /**
     * 修改章节状态
     * 状态，0:隐藏；1：待更新；2：已发布
     *
     * @param id     章节id
     * @param status 章节状态
     * @return
     */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(Integer id, Integer status) {

        courseContentService.updateSectionStatus(id, status);
        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        return new ResponseResult(true, 200, "修改章节状态成功", map);
    }

    /**
     * 保存&修改课时
     *
     * @param courseLesson
     * @return
     */
    @RequestMapping("/saveOrUpdateLesson")
    public ResponseResult saveOrUpdateLesson(@RequestBody CourseLesson courseLesson) {

        courseContentService.saveLesson(courseLesson);
        return new ResponseResult(true, 200, "新建课时成功", null);
    }
}
